import { stringToBytes } from "../../utils/weichatPb/src/util";

// Componet/Componet.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    propArray: {
      type: Array,
    },
    nameSearch:{
      type: Boolean,
      value: true
    },
    top:{
      type:String,
      value:'210'
    },
    filter:{
      type:Boolean,
      value:false
    },
    filterArr: {
      type: Array,
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    filterIndex:0,
    cityShow: '0',
    provIndex: 0,
    cityIndex: 0,
    regionIndex: 0,
    provId: 0,
    cityId: 0,
    regionId: 0,
    provName: '全部省/市',
    cityName: '全部市/区',
    regionName: '全部区/县',
    prov_animationData: {}, 
    city_animationData: {}, 
    region_animationData: {}
  },
  /**
   * 组件的方法列表
   */
  methods: {　　　 //option的显示与否
    inputName: function (e) {
      clearTimeout(this.data.timer)
      // let inputName = e.detail.value
      this.data.timer = setTimeout(() => {
        console.log(e.detail.value)
        this.setData({
          inputName: e.detail.value
        })
      }, 500);
      // this.setData({
      //   timer: timer
      // })
    },
    /**
     * 附加筛选条件变化
     */
    pickerChange:function (e) {
      let filterIndex = e.detail.value;
      this.setData({
        filterIndex: filterIndex,
        inputName:''
      })
      let idArray = [this.data.provId, this.data.cityId, this.data.regionId];
      this.triggerEvent('filterevent', {idArray:idArray,filterIndex:filterIndex});
    },
    searchHospital:function(){
      let that = this;
      that.setData({
        provName: '全部省/市',
        cityName: '全部市/区',
        regionName: '全部区/县',
        filterIndex:0,
      })
      if (that.data.provId != 0 || that.data.cityId != 0 || that.data.regionId != 0) {
        that.setData({
          provId: 0,
          cityId: 0,
          regionId: 0,
        })
      }
      if(!that.data.inputName){
        let idArray = [that.data.provId, that.data.cityId, that.data.regionId]
        that.triggerEvent('myevent', idArray)
      }else {
        that.triggerEvent('searchevent', that.data.inputName)
      }
    },
    focusEvent: function(){
      this.cancelSelect()
    },
    provToggle: function() {
      this.pickUp()
      if (this.data.cityShow=='1') {
        this.setData({
          cityShow: '0',
        })
      } else {
        this.toggleAnimation(180)
        this.setData({
          cityShow: '1',
        })
      }
      this.setData({
        prov_animationData: this.animation.export()
      })
    },
    cityToggle: function() {
      this.pickUp()
      if (this.data.cityShow=='2') {
        this.setData({
          cityShow: '0',
        })
      } else {
        this.toggleAnimation(180)
        this.setData({
          cityShow: '2',
        })
      }
      this.setData({
        city_animationData: this.animation.export()
      })
    },
    regionToggle: function() {
      this.pickUp()
      if (this.data.cityShow == '3') {
        this.setData({
          cityShow: '0',
        })
      } else {
        this.toggleAnimation(180)
        this.setData({
          cityShow: '3',
        })
      }
      this.setData({
        region_animationData: this.animation.export()
      })
    },
    //创建动画

    toggleAnimation: function(angle) {
      var animation = wx.createAnimation({
        timingFunction: "ease"
      })
      this.animation = animation;
      animation.rotate(angle).step()
    },
    //设置内容
    setProv: function(e) {
      console.log("【prov>>>】",e)
      this.setData({
        inputName: ''
      })
      let provIndex = e.target.dataset.index; //当前点击的索引
      let provName = e.target.dataset.provName; 
      let provId = e.target.dataset.id; 
      this.toggleAnimation(0)
      this.setData({
        cityShow: '0',
        provIndex,
        provName,
        cityName: '全部市/区', 
        regionName: '全部区/县', 
        provId,
        cityIndex: 0,
        cityId:0,
        regionIndex: 0,
        regionId:0,
        prov_animationData: this.animation.export()
      })
      let idArray = [this.data.provId, this.data.cityId, this.data.regionId]
      this.triggerEvent('myevent', idArray)
    },
    setCity: function (e) {
      console.log("【city>>>】",e)
      this.setData({
        inputName: ''
      })
      let cityIndex = e.target.dataset.index; //当前点击的索引
      let cityName = e.target.dataset.cityName;
      let provIndex = this.data.provIndex
      let cityId = e.target.dataset.cityId;
      this.toggleAnimation(0)
      this.setData({
        cityShow: '0',
        cityIndex,
        cityId,
        cityName,
        regionName: '全部区/县', 
        regionIndex: 0,
        regionId:0,
        city_animationData: this.animation.export()
      })
      let idArray = [this.data.provId, this.data.cityId, this.data.regionId]
      this.triggerEvent('myevent', idArray)
    },
    setRegion: function (e) {
      console.log(e)
      this.setData({
        inputName: ''
      })
      let regionIndex = e.target.dataset.index; //当前点击的索引
      let regionName = e.target.dataset.regionName;
      let regionId = e.target.dataset.regionId;
      this.toggleAnimation(0)
      this.setData({
        cityShow: '0',
        regionIndex,
        regionId,
        regionName,
        region_animationData: this.animation.export()
      })
      let idArray = [this.data.provId, this.data.cityId, this.data.regionId]
      this.triggerEvent('myevent', idArray)
    },
    cancelSelect: function(){
      this.pickUp()
      this.setData({
        cityShow: '0'
      })
    },
    pickUp: function(){
      if (this.data.cityShow == '1') {
        this.toggleAnimation(0)
        this.setData({
          prov_animationData: this.animation.export()
        })
      } else if (this.data.cityShow == '2') {
        this.toggleAnimation(0)
        this.setData({
          city_animationData: this.animation.export()
        })
      } else if (this.data.cityShow == '3') {
        this.toggleAnimation(0)
        this.setData({
          region_animationData: this.animation.export()
        })
      }
    }
  }
})